package cn.yuanqiao.archive.appManager.service.impl;/**
 * @author 赵常飞
 * @date 2024/6/4 14:00
 */

import cn.yuanqiao.archive.appManager.domain.TenantRyMjDzb;
import cn.yuanqiao.archive.appManager.domain.TenantSysDictData;

import cn.yuanqiao.archive.appManager.mapper.TenantMijiComparisonTableMapper;
import cn.yuanqiao.archive.appManager.service.ITenantMijiComparisonTableService;
import cn.yuanqiao.common.core.domain.entity.SysUser;
import cn.yuanqiao.common.utils.SecurityUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/4 14:00
 *@Description:
 *@Note:
 */
@Service
public class TenantMijiComparisonTableServiceImpl implements ITenantMijiComparisonTableService {
    @Autowired
    private TenantMijiComparisonTableMapper tenantMijiComparisonTableMapper;

    @Autowired
    private TenantSysDictTypeServiceImpl tenantSysDictTypeService;

    @Value("${FBswitch}")
    private boolean FBswitch;
    @Override
    public List<TenantSysDictData> getrymjdict() {
        return tenantSysDictTypeService.selectDictDataByType("RYMJ");
    }

    @Override
    public List<TenantSysDictData> getdamjdict() {
        return tenantSysDictTypeService.selectDictDataByType("MJ");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(JSONObject json) {
        JSONArray array= JSON.parseArray(json.getString("data"));
        TenantRyMjDzb ryMjDzb=new TenantRyMjDzb();
        String tenantCode = SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        ryMjDzb.setTableName(tenantCode+TenantRyMjDzb.BIGTABLENAME);
        tenantMijiComparisonTableMapper.deleteAll(ryMjDzb.getTableName());
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsoni=JSON.parseObject(String.valueOf(array.get(i)));
            String rymj=jsoni.getString("rymj");
            JSONArray damj = jsoni.getJSONArray("damj");
            for (int j = 0; j < damj.size(); j++) {
                ryMjDzb.setDaMj(String.valueOf(damj.get(j)));
                ryMjDzb.setRyMj(String.valueOf(rymj));
                tenantMijiComparisonTableMapper.add(ryMjDzb,ryMjDzb.getTableName());
            }

        }
        return 1;
    }

    @Override
    public List<TenantRyMjDzb> getdamjbyrymj() {
        List<TenantRyMjDzb> damjs =new ArrayList<TenantRyMjDzb>();
        if(FBswitch){
            //获取登录人密级
            String rymj = SecurityUtils.getLoginUser().getSysUser().getMiJi();
            TenantRyMjDzb tenantRyMjDzb=new TenantRyMjDzb();
            tenantRyMjDzb.setRyMj(rymj);
            String tenantCode = SecurityUtils.getLoginUser().getSysUser().getTenantCode();
            tenantRyMjDzb.setTableName(tenantCode+TenantRyMjDzb.BIGTABLENAME);
            damjs=tenantMijiComparisonTableMapper.getdamjbyrymj(tenantRyMjDzb);
        }else{

            List<TenantSysDictData> mj = tenantSysDictTypeService.selectDictDataByType("MJ");
            for (int i = 0; i < mj.size(); i++) {
                TenantRyMjDzb tenantRyMjDzb = new TenantRyMjDzb();
                tenantRyMjDzb.setDaMj(mj.get(i).getDictValue());
                damjs.add(tenantRyMjDzb);
            }
        }

        return damjs;
    }

    @Override
    public List<TenantRyMjDzb> getdamjbyrymjCode(SysUser user,String code) {
        List<TenantRyMjDzb> damjs =new ArrayList<TenantRyMjDzb>();
        if(FBswitch){
            //获取登录人密级
            String rymj = user.getMiJi();
            TenantRyMjDzb tenantRyMjDzb=new TenantRyMjDzb();
            tenantRyMjDzb.setTableName(code.toUpperCase()+"_MIJICOMPARISON");
            tenantRyMjDzb.setRyMj(rymj);
            tenantRyMjDzb.setTableName(code+TenantRyMjDzb.BIGTABLENAME);
            damjs=tenantMijiComparisonTableMapper.getdamjbyrymj(tenantRyMjDzb);
        }else{

            List<TenantSysDictData> mj = tenantSysDictTypeService.selectDictDataByType("MJ");
            for (int i = 0; i < mj.size(); i++) {
                TenantRyMjDzb tenantRyMjDzb = new TenantRyMjDzb();
                tenantRyMjDzb.setDaMj(mj.get(i).getDictValue());
                damjs.add(tenantRyMjDzb);
            }
        }
        return damjs;
    }

    @Override
    public String determine(JSONObject json) {
        String msg="ok";
        if(!FBswitch){
            return msg;
        }
        String daMj = json.getString("daMj");
        JSONArray wjMjs = json.getJSONArray("wjMjs");
        List<Integer> wjMjlist=wjMjs.stream().mapToInt(value1 -> Integer.parseInt(value1.toString())).boxed().collect(Collectors.toList());
        List<TenantSysDictData> mj = tenantSysDictTypeService.selectDictDataByType("MJ");
        String chinesedamj="";
        for (int i = 0; i < mj.size(); i++) {
            String dictvalue=mj.get(i).getDictValue();
            if(daMj.equals(dictvalue)){
                chinesedamj=mj.get(i).getDictLabel();
            }
        }
        if(null==chinesedamj||"".equals(chinesedamj)){
            return "未匹配到字典";
        }
        List<Integer> allwjmiji=null;
        if("无秘".equals(chinesedamj)){
            allwjmiji=new ArrayList(){{
                add(0);
                add(4);
            }};
        }else if("秘密".equals(chinesedamj)){
            allwjmiji=new ArrayList(){{
                add(0);
                add(1);
                add(4);
            }};
        }else if("机密".equals(chinesedamj)) {
            allwjmiji = new ArrayList() {{
                add(0);
                add(1);
                add(2);
                add(4);
            }};
        }else if("绝密".equals(chinesedamj)){
            allwjmiji = new ArrayList() {{
                add(0);
                add(1);
                add(2);
                add(3);
                add(4);
            }};
        }else if("内部".equals(chinesedamj)){
            allwjmiji = new ArrayList() {{
                add(0);
            }};
        }
        List<Integer> list1=new ArrayList<>(allwjmiji.size()+wjMjlist.size());
        list1.addAll(allwjmiji);
        list1.addAll(wjMjlist);
        List<Integer> collect = list1.stream().distinct().collect(Collectors.toList());
        if(collect.size()>allwjmiji.size()){
            msg="文件密级大于档案密级,请重新选择!";
        }
        return msg;
    }

    @Override
    public List<Map<String, String>> getdamjbyrymjforsys() {
        List<Map<String, String>> damjs =new ArrayList<Map<String, String>>();
        if(FBswitch){
            String rymj = SecurityUtils.getLoginUser().getSysUser().getMiJi();
            TenantRyMjDzb tenantRyMjDzb=new TenantRyMjDzb();
            tenantRyMjDzb.setRyMj(rymj);
            String tenantCode = SecurityUtils.getLoginUser().getSysUser().getTenantCode();
            tenantRyMjDzb.setTableName(tenantCode+TenantRyMjDzb.BIGTABLENAME);
            damjs=tenantMijiComparisonTableMapper.getdamjbyrymjforsys(tenantRyMjDzb);
        }else{
            List<TenantSysDictData> mj = tenantSysDictTypeService.selectDictDataByType("MJ");
            for (int i = 0; i < mj.size(); i++) {
                Map<String, String> tenantRyMjDzb = new HashMap<String, String>();
                tenantRyMjDzb.put("DA_MJ",mj.get(i).getDictValue());
                damjs.add(tenantRyMjDzb);
            }
        }

        return damjs;
    }

    @Override
    public List<TenantRyMjDzb> getmjdzb() {
        TenantRyMjDzb tenantRyMjDzb=new TenantRyMjDzb();
        String tenantCode = SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        tenantRyMjDzb.setTableName(tenantCode+TenantRyMjDzb.BIGTABLENAME);
        return tenantMijiComparisonTableMapper.getdamjbyrymj(tenantRyMjDzb);
    }
}
